﻿using System;
using UnityEngine.EventSystems;

namespace KuiHuaBaoDian.Services.UGUI {

    public interface IUIBehaviour : IMonoBehaviour {
    }

    public abstract class KHBD_UIBehaviour : UIBehaviour, IUIBehaviour, IEntity {

        protected KHBD_UIBehaviour() : base() { }

        public event IObject.DisposingDelegate Disposing;

        public bool IsDisposed { get; private set; }

        public bool IsDisposing { get; private set; }

        protected override void Awake() {
            base.Awake();
            AutowireUtility.Autowire(this);
        }

        protected sealed override void OnDestroy() {
            if (!IsDisposed) {
                if (Disposing != null) {
                    Disposing.Invoke();
                    Disposing = null;
                }
                IsDisposing = true;
                Dispose();
                IsDisposed = true;
                IsDisposing = false;
                AutowireUtility.Dispose(this);
            }
            base.OnDestroy();
        }

        protected virtual void Dispose() {
        }

        ///// <summary>
        ///// 修正TMP_Text中maskable=false的情况下还是无法正确显示的问题
        ///// </summary>
        ///// <param name="text">需要修正的<see cref="TMP_Text"/>组件</param>
        //protected void FixTextMaskable(TMP_Text text) {
        //    if (text != null && !text.maskable) {
        //        text.OnPreRenderText += _ => {
        //            var subMeshUIs = text.GetComponentsInChildren<TMP_SubMeshUI>(true);
        //            foreach (var subMeshUI in subMeshUIs) {
        //                subMeshUI.maskable = false;
        //            }
        //        };
        //    }
        //}
    }
}
